package Design_Pattern;

/**
 * @author zzq
 * time:2018/8/21 1:02
 * 两种使用方法编写多线程下的singletton模式，都是线程安全的
 */
public class SingletonForMoreThreads {

   private SingletonForMoreThreads(){

   }

//第一种：使用synchronized关键字
private SingletonForMoreThreads singletonForMoreThreads;
    public synchronized SingletonForMoreThreads  createObject(){
       if(null ==singletonForMoreThreads){
           singletonForMoreThreads = new SingletonForMoreThreads();
       }
       return  singletonForMoreThreads;
    }
    //第二种：使用volatile关键字
    private volatile SingletonForMoreThreads singletonForMoreThreads2;
    public SingletonForMoreThreads createObject2(){

        synchronized(SingletonForMoreThreads.class){ //SingletonForMoreThreads.class可以任意
            singletonForMoreThreads2 =  new SingletonForMoreThreads();
        }
        return singletonForMoreThreads2;
    }

}
